import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:async';

void main() => runApp(MyApp());

/// 对Stream的封装:数据加工的工具
/// Observable->基于Stream并扩展其他功能，新版本废弃
/// Subject->StreamController
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'RxDart',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  PublishSubject _textFieldSubject;

  @override
  void initState() {
    super.initState();
    // 创建Observable(新版本直接使用Stream)
    // Stream stream = Stream.fromIterable(["hello", "Java", "Dart"]);

    // _createPublishSubject();
    // _createBehaviorSubject();
    // _createReplaySubject();
    _textFieldSubject = PublishSubject();
    _textFieldSubject
        // 事件必须间隔2s才会被使用
        .debounceTime(Duration(seconds: 2))
        // 数据转换
        .map((event) => 'item:$event')
        // 数据过滤
        .where((event) => event.length > 16)
        .listen(print);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RxDart'),
      ),
      body: Container(
        child: Column(
          children: [
            TextField(
              onChanged: (value) => _textFieldSubject.add("onChange:$value"),
              onSubmitted: (value) => _textFieldSubject.add("onSubmit:$value"),
              decoration: InputDecoration(
                labelText: 'Name',
                labelStyle: TextStyle(color: Colors.black),
                filled: true,
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 先监听再发送数据，才会被接收到
  void _createPublishSubject() {
    // 创建Subject
    PublishSubject<String> _subject = PublishSubject<String>();
    // 监听Stream
    _subject.listen((value) {
      print('listen1:$value');
    });
    _subject.add("hello");
    // listen2不会监听到hello，但是listen1会监听到world，先订阅在发送才会监听到
    _subject.listen((value) {
      print('listen2:${value.toUpperCase()}');
    });
    _subject.add("world");

    _subject.close();
  }

  // 把最后一个数据作为第一项数据交给新来的监听
  void _createBehaviorSubject() {
    BehaviorSubject<String> _subject = BehaviorSubject<String>();
    // 先发送两个数据，只把最后一项交给新来的listen1、listen2监听，而后发送的数据 listen1、listen2都会监听到
    _subject.add("hello");
    _subject.add("world");
    _subject.listen((value) {
      print('listen1:$value');
    });

    _subject.listen((value) {
      print('listen2:${value.toUpperCase()}');
    });
    _subject.add("finish");
    _subject.close();
  }

  // 先发送的数据然后再监听都会接收到
  void _createReplaySubject() {
    // 指定最大能接收到的消息个数，只能接收后两个发送的消息
    ReplaySubject<String> _subject = ReplaySubject<String>(maxSize: 2);
    _subject.add("hello");
    _subject.add("world");
    _subject.add("Java");
    _subject.listen((value) {
      print('listen1:$value');
    });

    _subject.listen((value) {
      print('listen2:${value.toUpperCase()}');
    });
    _subject.close();
  }
}
